package com.ace3l.legal.backend.domain;


import javax.persistence.*;
import java.util.Set;
import java.util.HashSet;

/**
 * ACE3L.COM
 * com.ace3l.manager.domain
 * This code was written by Synesthesia Consulting Services in conjunction
 * with Ace3L.com. This code is confidential and proprietary. This code is
 * copyright (C) 2008  Synesthesia Consulting Services and Ace3L.com
 * Created By: ageer
 * Date: Jun 14, 2008
 * Time: 8:49:14 PM
 */
@Entity
@org.hibernate.annotations.GenericGenerator(
    name = "hibernate-increment",
    strategy = "increment"
)
@Table(name = "USER")
@NamedQueries({
    @NamedQuery(name="user.byusername",query="from User u where u.username = ?"),
    @NamedQuery(name="user.byid",query="from User u where u.id = :id")
})
public class User {
    @Transient
    public static User EMPTY_USER = new User();
    
    @Id @GeneratedValue(generator = "hibernate-increment")
    @Column(name="ID")
    private Integer id;

    @Column(name="SALT")
    private byte[] salt;

    @Column(name="HASH")
    private byte[] hash;

    @Column(name="USERNAME")
    private String username;

    @Transient
    private String password;

    @OneToMany(
            cascade = CascadeType.ALL,
            fetch = FetchType.EAGER,
            mappedBy = "user")
    private Set<Role> roles = new HashSet();


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

    public void addRole(Role role) {
        role.setUser(this);
        role.setUsername(this.getUsername());
        roles.add(role);
    }

    public byte[] getSalt() {
        return salt;
    }

    public void setSalt(byte[] salt) {
        this.salt = salt;
    }

    public byte[] getHash() {
        return hash;
    }

    public void setHash(byte[] hash) {
        this.hash = hash;
    }

    public String getFirstRole() {
        String myRole = roles.iterator().next().getRole();
        return myRole;
    }
    public String toString() {
        return "[" + id + "]"+ username;          
    }
}
